package com.shmilyou.dissertationmanagement.web;
/* Created with 岂止是一丝涟漪     530060499@qq.com    2019/2/9 */

import com.shmilyou.dissertationmanagement.biz.ThesisPublishTypeService;
import com.shmilyou.dissertationmanagement.domain.ThesisPublishType;
import com.shmilyou.dissertationmanagement.tools.Utils;
import com.shmilyou.dissertationmanagement.utils.WebUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;
import java.util.Map;

@Controller
public class ThesisPublishTypeController {

    private final ThesisPublishTypeService thesisPublishTypeService;

    @Autowired
    public ThesisPublishTypeController(ThesisPublishTypeService thesisPublishTypeService) {
        this.thesisPublishTypeService = thesisPublishTypeService;
    }

    @RequestMapping("/thesisPublishType")
    public String index(Map<Object, Object> map) {
        List<ThesisPublishType> entities = thesisPublishTypeService.queryAll();
        map.put("entities", entities);
        return "thesisPublishType";
    }

    @RequestMapping(value = "/thesisPublishType/rm", method = RequestMethod.POST)
    public ResponseEntity<Map<String, Object>> delete(String id) {
        if (Utils.isInteger(id)) {
            ThesisPublishType exit = thesisPublishTypeService.queryById(Integer.parseInt(id));
            if (exit != null) {
                thesisPublishTypeService.delete(exit);
                return WebUtils.success();
            }
        }
        return WebUtils.failure();
    }

    @RequestMapping(value = "/thesisPublishType/edit", method = RequestMethod.GET)
    public String edit(String id, ModelMap map) {
        if (Utils.isInteger(id)) {
            ThesisPublishType entity = thesisPublishTypeService.queryById(Integer.parseInt(id));
            map.put("entity", entity);
            return "thesisPublishType_edit";
        }
        return "error";
    }

    @RequestMapping(value = "/thesisPublishType/add", method = RequestMethod.GET)
    public String add() {
        return "thesisPublishType_add";
    }

    @RequestMapping(value = "/thesisPublishType/edit", method = RequestMethod.POST)
    public ResponseEntity<Map<String, Object>> editHandle(ThesisPublishType entity) {
        thesisPublishTypeService.update(entity);
        return WebUtils.success();
    }

    @RequestMapping(value = "/thesisPublishType/add", method = RequestMethod.POST)
    public ResponseEntity<Map<String, Object>> addHandle(ThesisPublishType entity) {
        thesisPublishTypeService.save(entity);
        return WebUtils.success();
    }
}
